<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Models\Level;
use App\Http\Resources\LevelResource;
use Illuminate\Http\Request;

class LevelController extends Controller
{


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //最新提交（解决无限级显示及添加功能）
        $levels = LevelResource::collection(Level::all());
//        dump($levels[1]);
        return view('level.index', compact('levels'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $levels = Level::all();
        return view('level.create', compact('levels'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */

    public function store(Request $request, Level $level)
    {
        $level->name = $request->name;
        $level->pid = $request->id;
        $level->save();

        $level->path = $level->getPath();
        $level->level = $level->getLevel();
        $level->save();

        return response()->json(['msg' => '创建成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param \App\Level $level
     * @return \Illuminate\Http\Response
     */
    public function show(Level $level)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param \App\Level $level
     * @return \Illuminate\Http\Response
     */
    public function edit(Level $level)
    {
        $levels = Level::all();
        return view('level.edit', compact('level', 'levels'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param \App\Level $level
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Level $level)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param \App\Level $level
     * @return \Illuminate\Http\Response
     */
    public function destroy(Level $level)
    {
        //
    }
}
